package com.sczmgk.hotline_pzh.photo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.photo.adapter.PhotoAdappter;
import com.sczmgk.hotline_pzh.photo.entity.PhotoAlbum;
import com.sczmgk.hotline_pzh.photo.entity.PhotoItem;
import com.sczmgk.hotline_pzh.photo.entity.PictureData;
import com.sczmgk.hotline_pzh.utils.MyLog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 相册Activity
 *
 * @author bo
 */
public class PhotoActivity extends BaseActivity implements OnClickListener {

    public static final int CHOOSE_MAX_COUNT = 5;
    @Override
    public boolean getIsNeedLogin() {
        return true;
    }

    /**
     * 表示该Activity可以接受的参数
     *
     * @author EX-LUOCHUN
     * @version [Android PABank C01, 2011-12-24]
     */
    public static class Param {

        /**
         * 容量大小
         */
        public static final String CHOOSE_MAX_SIZE = "choose_max_size";
        /**
         * 相册详情
         */
        public static final String AIBUM_INFO = "aiblum_info";

    }

    /**
     * 本类返回的详细信息
     *
     * @author luochun
     */
    public static class ResultParam {
        /**
         * 自定义相册返回
         */
        public static final int REQUEST_CODE_SELECT_PHOTO = 1040;
        /**
         * 相册详情
         */
        public static final String AIBUM_INFO = "latitude";

        /**
         * 当前用户选择的相片
         */
        public static final String SELECT_PHOTO_LIST = "longitude";
    }

    /**
     * gridview
     */
    private GridView mGridView;
    /**
     * 当前相册
     */
    private PhotoAlbum mAibum;
    /**
     * adapter
     */
    private PhotoAdappter adapter;
    /**
     * 最大选择数量
     */
    private int maxChooseSize = 0;
    /**
     * 当前选择中的图片
     */
    private List<PhotoItem> mSelectPhotoList;

    public static final int REQUEST_PHOTO_DETAIL = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photoalbum_gridview);
        initHeadStyle();
        initData();
        initView();
    }

    /**
     * 初始化头部信息
     */
    private void initHeadStyle() {
        setTitle(R.string.photo_album_choice_title);
        setRightBtnVisibility(View.VISIBLE);
        setRightBtnBackground(android.R.color.transparent, String.format(getString(R.string.sure_choose), 0+""));
        setRightBtnClickListener(this);
        setLeftBtnClickListener(this);
    }

    /**
     * 初始化数据
     */
    @SuppressWarnings("unchecked")
    private void initData() {
        //目前用户选择的图片列表
        mSelectPhotoList = (List<PhotoItem>) getIntent().getExtras().get(Constants.SELECTED_PIC_LIST);

        //设置选中图片的数量/
        setRightBtnBackground(android.R.color.transparent, String.format(getString(R.string.sure_choose), mSelectPhotoList.size()+""));
        //当前相册信息
        mAibum = (PhotoAlbum) getIntent().getExtras().get(Param.AIBUM_INFO);
        //更新相册信息
        if(mSelectPhotoList != null && mSelectPhotoList.size()>0){

            for (PhotoItem photo : mSelectPhotoList) {

                for (PhotoItem photoItem : mAibum.getBitList()) {
                    if(photoItem.getPhotoPath().equals(photo.getPhotoPath())){
                        photoItem.setSelect(true);
                        break;
                    }
                }
            }
        }

        //最大选择数量
        maxChooseSize = (Integer) getIntent().getExtras().get(Param.CHOOSE_MAX_SIZE);
    }

    /**
     *
     */
    private void initView() {

        mGridView = (GridView) findViewById(R.id.photo_gridview);
        adapter = new PhotoAdappter(this, mAibum);
        mGridView.setAdapter(adapter);
    }

    /**
     * 设置图片被选择
     *
     * @param clickPhotoItem
     */
    public void setPhotoCheck(PhotoItem clickPhotoItem) {
        if (clickPhotoItem.isSelect()) {
            clickPhotoItem.setSelect(false);
            removeSelectPhoto(clickPhotoItem);
        } else {
            if (1 + getSelectSize() > maxChooseSize) {
                Toast.makeText(PhotoActivity.this, String.format(getString(R.string.photo_max_choose), maxChooseSize+""),
                        Toast.LENGTH_SHORT).show();
                return;
            }
            clickPhotoItem.setSelect(true);
            addSelectPhoto(clickPhotoItem);
        }
        if (mSelectPhotoList != null) {
            //选择后的图片
            setRightBtnBackground(android.R.color.transparent, String.format(getString(R.string.sure_choose), mSelectPhotoList.size()+""));
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 从选择的列表里面 删除某个取消选择的图片
     *
     * @param removePhoto
     */
    private void removeSelectPhoto(PhotoItem removePhoto) {
        if (mSelectPhotoList != null) {
            for (int i = 0; i < mSelectPhotoList.size(); i++) {
                PhotoItem photoItem = mSelectPhotoList.get(i);
                if (photoItem.getPhotoPath().equals(removePhoto.getPhotoPath())) {
                    mSelectPhotoList.remove(photoItem);
                }
            }
        }
    }

    /**
     * 加入选择的图片
     *
     * @param removePhoto
     */
    private void addSelectPhoto(PhotoItem removePhoto) {
        if (mSelectPhotoList == null) {
            mSelectPhotoList = new ArrayList<PhotoItem>();
        }
        mSelectPhotoList.add(removePhoto);
    }

    /**
     * @return
     */
    public int getSelectSize() {
        if (mSelectPhotoList == null) {
            return 0;
        }
        return mSelectPhotoList.size();
    }

    /**
     * @return
     */
    public int getMaxSelectSize() {
        return maxChooseSize;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_left_title:
                closeActivity();
                break;
            case R.id.btn_right_title:
                Intent intent = new Intent();
                intent.putExtra(ResultParam.AIBUM_INFO, mAibum);
                intent.putExtra(ResultParam.SELECT_PHOTO_LIST, (Serializable) mSelectPhotoList);
                MyLog.d(TAG, "setResult     resultCode: " + ResultParam.REQUEST_CODE_SELECT_PHOTO);
                setResult(ResultParam.REQUEST_CODE_SELECT_PHOTO, intent);
                finish();
                break;
        }
    }

    private void closeActivity() {
//        Intent intent1 = new Intent();
//        intent1.putExtra(ResultParam.AIBUM_INFO, mAibum);
//        intent1.putExtra(ResultParam.SELECT_PHOTO_LIST, (Serializable) mSelectPhotoList);
//        setResult(ResultParam.REQUEST_CODE_SELECT_PHOTO, intent1);
        finish();
    }

    @Override
    public void onBackPressed() {
        closeActivity();
    }

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
        super.onActivityResult(requestCode, resultCode, arg2);
        if (requestCode == REQUEST_PHOTO_DETAIL) {
            ArrayList<PictureData> pictures = (ArrayList<PictureData>) arg2.getSerializableExtra("select_list");
            if (pictures.size() == mAibum.getBitList().size()) {
                for (int i = 0; i < pictures.size(); i++) {
                    PictureData pictureData = pictures.get(i);
                    PhotoItem photoItem = mAibum.getBitList().get(i);
                    if (pictureData.isSelect() != photoItem.isSelect()) {
                        if (pictureData.isSelect()) {
                            photoItem.setSelect(true);
                            addSelectPhoto(photoItem);
                        } else {
                            photoItem.setSelect(false);
                            removeSelectPhoto(photoItem);
                        }
                    }
                }
                if (mSelectPhotoList != null) {
                    setRightBtnBackground(android.R.color.transparent, String.format(getString(R.string.sure_choose), mSelectPhotoList.size()+""));
                }
                adapter.notifyDataSetChanged();
            }

            if (resultCode == RESULT_OK) {
                Intent intent1 = new Intent();
                intent1.putExtra(ResultParam.AIBUM_INFO, mAibum);
                intent1.putExtra(ResultParam.SELECT_PHOTO_LIST, (Serializable) mSelectPhotoList);
                setResult(RESULT_OK, intent1);
                finish();
            }

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            closeActivity();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
